﻿<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:Converters="clr-namespace:MahApps.Metro.Converters"
                    xmlns:system="clr-namespace:System;assembly=mscorlib">

    <Converters:ToUpperConverter x:Key="ToUpperConverter" />

    <Style x:Key="{ComponentResourceKey ResourceId=MetroDataGridSelectAllButtonStyle, TypeInTargetAssembly={x:Type DataGrid}}" TargetType="{x:Type Button}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid>
                        <Rectangle x:Name="Border" SnapsToDevicePixels="True">
                            <Rectangle.Resources>
                                <Style TargetType="{x:Type Rectangle}">
                                    <Setter Property="Fill" Value="{DynamicResource GrayBrush5}"/>
                                </Style>
                            </Rectangle.Resources>
                        </Rectangle>
                        <Polygon x:Name="Arrow" Fill="{DynamicResource GrayBrush3}" HorizontalAlignment="Right" Margin="8,8,3,3" Opacity="0.15" Points="0,10 10,10 10,0" Stretch="Uniform" VerticalAlignment="Bottom"/>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter Property="Fill" TargetName="Border">
                                <Setter.Value>
                                    <SolidColorBrush Color="{DynamicResource AccentColor2}"/>
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter Property="Visibility" TargetName="Arrow" Value="Collapsed"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="MetroColumnHeaderGripperStyle" TargetType="{x:Type Thumb}">
        <Setter Property="Width" Value="8" />
        <Setter Property="Background" Value="{DynamicResource GrayBrush4}"/>
        <Setter Property="Cursor" Value="SizeWE"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Thumb}">
                    <Border Padding="{TemplateBinding Padding}" Background="Transparent">
                        <Rectangle HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Width="1" Fill="{TemplateBinding Background}" />
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="MetroRowHeaderGripperStyle" TargetType="{x:Type Thumb}">
        <Setter Property="Width" Value="8"/>
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="Cursor" Value="SizeNS"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Thumb}">
                    <Border Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="True"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="MetroDataGridColumnHeader" TargetType="{x:Type DataGridColumnHeader}">
        <Setter Property="SnapsToDevicePixels" Value="True" />
        <Setter Property="MinWidth" Value="0" />
        <Setter Property="MinHeight" Value="25" />
        <Setter Property="Foreground" Value="{DynamicResource BlackColorBrush}" />
        <Setter Property="Cursor" Value="Hand" />
        <Setter Property="HorizontalContentAlignment" Value="Left" />
        <Setter Property="VerticalContentAlignment" Value="Center" />
        <Setter Property="Padding" Value="10,4,4,7" />
        <Setter Property="Margin" Value="0,0,0,0"/>
        <Setter Property="BorderBrush" Value="{DynamicResource GrayBrush4}"/>
        <Setter Property="Background" Value="{DynamicResource GrayBrush8}"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>
                        <Border x:Name="BackgroundBorder" BorderThickness="0,0,0,3"
                                Grid.ColumnSpan="2" Background="{TemplateBinding Background}"
                                BorderBrush="{TemplateBinding BorderBrush}"/>

                        <ContentPresenter x:Name="HeaderContent"
                                          Content="{TemplateBinding Content}"
                                          ContentTemplate="{TemplateBinding ContentTemplate}"
                                          TextBlock.FontWeight="SemiBold"
                                          Margin="{TemplateBinding Padding}"
                                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                          RecognizesAccessKey="True"
                                          SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />


                        <Path x:Name="SortArrow"
                              Visibility="Collapsed"
                              Stretch="Fill" 
                              Grid.Column="1"
                              Width="8"
                              Height="10"
                              Margin="0,0,8,2" 
                              VerticalAlignment="Center"
                              RenderTransformOrigin="0.5,0.5"
                              Fill="{DynamicResource GrayBrush2}"/>

                        <Thumb x:Name="PART_LeftHeaderGripper"
                               HorizontalAlignment="Left" 
                               HorizontalContentAlignment="Left"
                               Background="Transparent"
                               Style="{StaticResource MetroColumnHeaderGripperStyle}"/>

                        <Thumb x:Name="PART_RightHeaderGripper"
                               HorizontalAlignment="Right"
                               HorizontalContentAlignment="Right"
                               Style="{StaticResource MetroColumnHeaderGripperStyle}" Grid.Column="1"/>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="SortDirection" Value="{x:Null}">
                            <Setter TargetName="BackgroundBorder" Property="Background" 
                                    Value="{DynamicResource WhiteColorBrush}" />
                        </Trigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsMouseOver" Value="True" />
                                <Condition Property="SortDirection" Value="{x:Null}" />
                            </MultiTrigger.Conditions>
                            <Setter TargetName="BackgroundBorder" Property="Background"
                                    Value="{DynamicResource GrayBrush8}"/>
                        </MultiTrigger>

                        <Trigger Property="SortDirection" Value="Ascending">
                            <Setter TargetName="SortArrow" Property="Visibility" Value="Visible" />
                            <Setter TargetName="SortArrow" Property="Data" Value="F1 M 34,57L 42,57L 42,32.25L 52,42.25L 52,31.75L 38,17.75L 24,31.75L 24,42.25L 34,32.25L 34,57 Z "/>
                        </Trigger>
                        <Trigger Property="SortDirection" Value="Descending">
                            <Setter TargetName="SortArrow" Property="Visibility" Value="Visible" />
                            <Setter TargetName="SortArrow" Property="Data" Value="F1 M 42,19.0002L 34,19.0002L 34,43.7502L 24,33.7502L 24,44.2502L 38,58.2502L 52,44.2502L 52,33.7502L 42,43.7502L 42,19.0002 Z "/>
                            <Setter TargetName="SortArrow" Property="Margin" Value="0,0,8,0"/>
                        </Trigger>
                        <Trigger Property="DisplayIndex" Value="0">
                            <Setter Property="Visibility" Value="Collapsed" 
                                    TargetName="PART_LeftHeaderGripper"></Setter>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="ContentTemplate">
            <Setter.Value>
                <DataTemplate DataType="{x:Type system:String}">
                    <TextBlock Text="{TemplateBinding Content, Converter={StaticResource ToUpperConverter}}"/>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="MetroDataGridRowHeader" TargetType="{x:Type DataGridRowHeader}">
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="BorderBrush" Value="{DynamicResource GrayBrush4}"/>
        <Setter Property="BorderThickness" Value="0,0,1,0"/>
        <Setter Property="Margin" Value="0,0,0,0"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridRowHeader}">
                    <Grid>
                        <Border  BorderBrush="{TemplateBinding BorderBrush}" 
                                 Background="{TemplateBinding Background}"                                     
                                 BorderThickness="{TemplateBinding BorderThickness}" 
                                 Padding ="{TemplateBinding Padding}" 
                                 Margin="{TemplateBinding Margin}"
                                 SnapsToDevicePixels="True">
                            <StackPanel Orientation="Horizontal">
                                <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
                                                  VerticalAlignment="Center"/>
                            </StackPanel>
                        </Border>
                        <Thumb x:Name="PART_TopHeaderGripper" 
                            VerticalAlignment="Top" Height="3" 
                            Style="{StaticResource MetroRowHeaderGripperStyle}"/>
                        <Thumb x:Name="PART_BottomHeaderGripper" 
                            VerticalAlignment="Bottom" Height="3" 
                            Style="{StaticResource MetroRowHeaderGripperStyle}"/>
                    </Grid>

                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background">
                                <Setter.Value>
                                    <SolidColorBrush Color="{DynamicResource AccentColor3}" />
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="MetroDataGridCell" TargetType="{x:Type DataGridCell}" >
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="BorderBrush" Value="Transparent"/>
        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
        <Setter Property="VerticalAlignment" Value="Stretch"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource AncestorType=DataGridRow, Mode=FindAncestor}, Path=Foreground}"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridCell}">
                    <Border BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            Background="{TemplateBinding Background}"
                            SnapsToDevicePixels="True">

                        <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="MetroDataGridRow" TargetType="{x:Type DataGridRow}">
        <Setter Property="MinHeight" Value="25"/>
        <Setter Property="Margin" Value="0,0,0,0"/>
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Background" Value="{DynamicResource AccentColorBrush}"/>
                <Setter Property="Foreground" Value="{DynamicResource AccentSelectedColorBrush}"/>
            </Trigger>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background">
                    <Setter.Value>
                        <SolidColorBrush Color="{DynamicResource AccentColor3}" />
                    </Setter.Value>
                </Setter>
            </Trigger>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="IsSelected" Value="True"/>
                    <Condition Property="Selector.IsSelectionActive" Value="True"/>
                </MultiTrigger.Conditions>
                <MultiTrigger.Setters>
                    <Setter Property="Background">
                        <Setter.Value>
                            <SolidColorBrush Color="{DynamicResource AccentColor2}"/>
                        </Setter.Value>
                    </Setter>
                </MultiTrigger.Setters>
            </MultiTrigger>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Foreground" Value="{DynamicResource GrayBrush5}" />
            </Trigger>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="IsEnabled" Value="False" />
                    <Condition Property="IsSelected" Value="True" />
                </MultiTrigger.Conditions>
                <MultiTrigger.Setters>
                    <Setter Property="Background" Value="{DynamicResource GrayBrush5}" />
                    <Setter Property="Foreground" Value="{DynamicResource AccentSelectedColorBrush}"/>
                </MultiTrigger.Setters>
            </MultiTrigger>
        </Style.Triggers>
    </Style>

    <Style x:Key="MetroDataGrid" TargetType="{x:Type DataGrid}">
        <Setter Property="GridLinesVisibility" Value="None"/>
        <Setter Property="Background" Value="{DynamicResource WhiteColorBrush}"/>
        <Setter Property="BorderBrush" Value="{DynamicResource AccentColorBrush}"/>
        <Setter Property="BorderThickness" Value="0,0,0,0"/>
        <Setter Property="ColumnHeaderStyle" Value="{StaticResource MetroDataGridColumnHeader}"/>
        <Setter Property="RowHeaderStyle" Value="{StaticResource MetroDataGridRowHeader}"/>
        <Setter Property="CellStyle" Value="{StaticResource MetroDataGridCell}"/>
        <Setter Property="RowStyle" Value="{StaticResource MetroDataGridRow}"/>
        <Setter Property="HeadersVisibility" Value="Column"/>
        <Setter Property="DropLocationIndicatorStyle">
            <Setter.Value>
                <Style TargetType="Separator">
                    <Setter Property="Background" Value="{DynamicResource GrayBrush5}"/>
                    <Setter Property="Width" Value="2"/>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="Separator">
                                <Rectangle Fill="{TemplateBinding Background}" Height="{TemplateBinding Height}" Width="{TemplateBinding Width}"/>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </Setter.Value>
        </Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGrid}">
                    <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" 
                            Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="True">
                        <ScrollViewer x:Name="DG_ScrollViewer" Focusable="false">
                            <ScrollViewer.Template>
                                <ControlTemplate TargetType="{x:Type ScrollViewer}">
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="Auto"/>
                                            <ColumnDefinition Width="*"/>
                                            <ColumnDefinition Width="Auto"/>
                                        </Grid.ColumnDefinitions>
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="Auto"/>
                                            <RowDefinition Height="*"/>
                                            <RowDefinition Height="Auto"/>
                                        </Grid.RowDefinitions>
                                        <Button Command="{x:Static DataGrid.SelectAllCommand}" Focusable="false" Style="{DynamicResource {ComponentResourceKey ResourceId=MetroDataGridSelectAllButtonStyle, TypeInTargetAssembly={x:Type DataGrid}}}" Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.All}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" Width="{Binding CellsPanelHorizontalOffset, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
                                        <DataGridColumnHeadersPresenter x:Name="PART_ColumnHeadersPresenter" Grid.Column="1" Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.Column}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" />
                                        <Grid Grid.Row="1" Grid.ColumnSpan="2">
                                            <ScrollContentPresenter x:Name="PART_ScrollContentPresenter" CanContentScroll="{TemplateBinding CanContentScroll}" Grid.ColumnSpan="2" Grid.Row="1"/>
                                        </Grid>
                                        <ScrollBar x:Name="PART_VerticalScrollBar" Grid.Column="2" Maximum="{TemplateBinding ScrollableHeight}" Orientation="Vertical" Grid.Row="1" Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" Value="{Binding VerticalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ViewportSize="{TemplateBinding ViewportHeight}"/>
                                        <Grid Grid.Column="1" Grid.Row="2">
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="{Binding NonFrozenColumnsViewportHorizontalOffset, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
                                                <ColumnDefinition Width="*"/>
                                            </Grid.ColumnDefinitions>
                                            <ScrollBar x:Name="PART_HorizontalScrollBar" Grid.Column="1" Maximum="{TemplateBinding ScrollableWidth}" Orientation="Horizontal" Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" Value="{Binding HorizontalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ViewportSize="{TemplateBinding ViewportWidth}"/>
                                        </Grid>
                                    </Grid>
                                </ControlTemplate>
                            </ScrollViewer.Template>
                            <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                        </ScrollViewer>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsGrouping" Value="true">
                <Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
            </Trigger>
        </Style.Triggers>
    </Style>

    <Style x:Key="AzureDataGridColumnHeader" TargetType="{x:Type DataGridColumnHeader}" BasedOn="{StaticResource MetroDataGridColumnHeader}">
        <Setter Property="Margin" Value="0,0,0,10"/>
    </Style>

    <Style x:Key="AzureDataGridRowHeader" TargetType="{x:Type DataGridRowHeader}" BasedOn="{StaticResource MetroDataGridRowHeader}">
        <Setter Property="BorderThickness" Value="0,0,0,0"/>
    </Style>

    <Style x:Key="AzureDataGridCell" TargetType="{x:Type DataGridCell}">
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="BorderBrush" Value="Transparent"/>
        <Setter Property="TextBlock.VerticalAlignment" Value="Center"/>
        <Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource AncestorType=DataGridRow, Mode=FindAncestor}, Path=Foreground}"/>
        <Setter Property="BorderThickness" Value="0"/>
        <Setter Property="SnapsToDevicePixels" Value="True"/>
        <Setter Property="MinHeight" Value="25"/>
        <Setter Property="VerticalAlignment" Value="Stretch"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridCell}">
                    <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"
                            Background="{TemplateBinding Background}" SnapsToDevicePixels="True"
                            VerticalAlignment="Stretch">
                        <ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}"
                                          Content="{TemplateBinding Content}"
                                          ContentStringFormat="{TemplateBinding ContentStringFormat}"
                                          SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                          VerticalAlignment="Center"
                                          Margin="2"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <DataTrigger
                        Binding="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=Column.DisplayIndex}"
                        Value="0">
                <Setter Property="Background">
                    <Setter.Value>
                        <SolidColorBrush Color="{DynamicResource AccentColor4}"/>
                    </Setter.Value>
                </Setter>
            </DataTrigger>
            <MultiDataTrigger>
                <MultiDataTrigger.Conditions>
                    <Condition Binding="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=Column.DisplayIndex}" Value="0"/>
                    <Condition Binding="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=IsSelected}" Value="True"/>
                </MultiDataTrigger.Conditions>
                <Setter Property="Background">
                    <Setter.Value>
                        <SolidColorBrush Color="{DynamicResource AccentColor2}"/>
                    </Setter.Value>
                </Setter>
            </MultiDataTrigger>
        </Style.Triggers>
    </Style>

    <Style x:Key="AzureDataGridRow" TargetType="{x:Type DataGridRow}" BasedOn="{StaticResource MetroDataGridRow}">
        <Setter Property="Margin" Value="0,0,0,1"/>
    </Style>

    <Style x:Key="AzureDataGrid" TargetType="{x:Type DataGrid}" BasedOn="{StaticResource MetroDataGrid}">
        <Setter Property="ColumnHeaderStyle" Value="{StaticResource AzureDataGridColumnHeader}"/>
        <Setter Property="CellStyle" Value="{StaticResource AzureDataGridCell}"/>
        <Setter Property="RowStyle" Value="{StaticResource AzureDataGridRow}"/>
        <Setter Property="RowHeaderStyle" Value="{StaticResource AzureDataGridRowHeader}"/>
    </Style>

</ResourceDictionary>
